import loginV from "../view/login.ejs";
import {postLogin} from "../api/admin";
export default ()=>{
	return (req, res, next) => {
		// render接收的内容即是呈现的内容
		res.render(loginV());
		document.querySelector("#loginBtn").onclick = async function(e){
			e.preventDefault();// 取消默认行为
			const adminName = document.loginForm.adminName.value;
			const password = document.loginForm.password.value;
			if(!/^[a-z]+$/i.test(adminName)){
				toastr.error('管理员账号必须全部是英文！');
				return;
			}
			if(!/^\w+$/.test(password)){
				toastr.error('密码只支持英文，数字，下划线');
				return;
			}
			const data = await postLogin({adminName,password});
			toastr.success(data.msg);
			localStorage.setItem("adminName",adminName);
			location.href = "/index/adminList"
		}
	}
}